package com.ns.scs.task.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.task.bo.ScheduledPageBO;
import com.ns.scs.task.bo.ScheduledTaskBO;
import com.ns.scs.task.dto.ScheduledPageDto;
import com.ns.scs.task.entity.ScheduledTask;
import com.ns.scs.task.service.IScheduledTaskService;
import com.ns.scs.task.vo.ScheduledTaskVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 定时任务-业务接口
 *
 * @author liao.wei
 * @since 2024-05-16
 */
@RestController
@RequestMapping("/scheduled")
@RequiredArgsConstructor
@CrossOrigin
public class ScheduledController {

    private final IScheduledTaskService scheduledTaskService;

    /**
     * 新增
     *
     * @param bo 参数对象
     * @return RoleVo
     */
    @PostMapping("add")
    @ReqLog(module = "任务调度模块", name = "增")
    public ResultOk add(@RequestBody ScheduledTaskBO bo) {
        ScheduledTask scheduledTask = new ScheduledTask();
        BeanUtils.copyProperties(bo, scheduledTask);
        scheduledTask.setSort(scheduledTaskService.count() + 1);
        return ResultOk.getByBoolean(scheduledTaskService.saveOrUpdate(scheduledTask));
    }

    /**
     * 更新
     *
     * @param bo 参数对象
     * @return RoleVo
     */
    @PostMapping("/update")
    @ReqLog(module = "任务调度模块", name = "改")
    public ResultOk update(@RequestBody @Validated ScheduledTaskBO bo) {
        ScheduledTask scheduledTask = new ScheduledTask();
        BeanUtils.copyProperties(bo, scheduledTask);
        return ResultOk.getByBoolean(scheduledTaskService.updateById(scheduledTask));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "任务调度模块", name = "逻辑删")
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(scheduledTaskService.removeById(id));
    }

    /**
     * 批量删除
     *
     * @param dto 参数
     * @return ResultOk
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "任务调度模块", name = "批量删")
    public ResultOk delete(@RequestBody IdsBO dto) {
        return ResultOk.getByBoolean(scheduledTaskService.removeBatchByIds(dto.getIds()));
    }

    /**
     * 查询详情
     *
     * @param id 参数
     * @return ResultOk
     */
    @GetMapping("getById")
    @ReqLog(module = "任务调度模块", name = "详情")
    public ResultOk find(Long id) {
        ScheduledTask scheduledTask = scheduledTaskService.getById(id);
        ScheduledTaskVO vo = new ScheduledTaskVO();
        BeanUtils.copyProperties(scheduledTask, vo);
        return ResultOk.getSuccessData(vo);
    }

    /**
     * 分页查询
     *
     * @param bo 参数
     * @return Result
     */
    @PostMapping("getList")
    @ReqLog(module = "任务调度模块", name = "查-集合")
    public ResultOk selectPage(@RequestBody @Validated ScheduledPageBO bo) {
        ScheduledPageDto dto = new ScheduledPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<ScheduledTaskVO> page = this.scheduledTaskService.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }

    /**
     * 执行任务
     *
     * @param id 参数
     * @return ResultOk
     */
    @GetMapping("exec")
    @ReqLog(module = "任务调度模块", name = "执行任务")
    public ResultOk execute(Long id) {
        this.scheduledTaskService.restart(id);
        return ResultOk.getSuccess();
    }
}